<template>
  <div class="modern-article-card bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700">
    <!-- 图片容器 -->
    <div class="image-container" @click="goToDetail(props.article?._id)">
      <el-image class="article-image" :src="props.article?.cover" fit="cover" :hide-on-click-modal="true" lazy>
        <template #placeholder>
          <div class="image-placeholder">
            <Loading />
          </div>
        </template>
      </el-image>
      <div class="image-overlay">
        <div class="read-more-btn">
          <i class="fas fa-eye"></i>
          <span>阅读全文</span>
        </div>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="card-content">
      <!-- 标签区域 -->
      <div class="tags-container" v-if="props.article?.tags?.length">
        <div class="tag-item" v-for="item in props.article?.tags?.slice(0, 2)" :key="item?._id">
          <el-image class="tag-icon" :src="item.icon" fit="fill" />
          <span class="tag-name">{{ item?.name }}</span>
        </div>
        <div class="more-tags" v-if="props.article?.tags?.length > 2">
          +{{ props.article?.tags?.length - 2 }}
        </div>
      </div>

      <!-- 标题 -->
      <h2 class="article-title" @click="goToDetail(props.article?._id)">
        {{ props.article?.title }}
      </h2>

      <!-- 描述 -->
      <p class="article-desc">{{ props.article?.desc }}</p>

      <!-- 分隔线 -->
      <div class="divider"></div>

      <!-- 元信息 -->
      <div class="article-meta">
        <div class="meta-item">
          <i class="fas fa-calendar-alt"></i>
          <span>{{ dayjs(props.article?.createTime).format("YYYY-MM-DD") }}</span>
        </div>
        <div class="meta-item">
          <i class="fas fa-folder"></i>
          <span class="category-name">{{ props.article?.aboutInfo.category.name }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps } from "vue"
import dayjs from "dayjs"
import { useRouter } from "vue-router"
import Loading from "@/components/Loading/index.vue"

// 全局路由对象
const router = useRouter()

// 接受父组件数据
const props = defineProps({
  article: {
    type: Object,
    default: () => ({})
  }
})

// 跳转到文章详情页面
const goToDetail = (id: string) => {
  router.push({
    path: "/home-detail",
    query: {
      id
    }
  })
}
</script>

<style scoped lang="scss">
.modern-article-card {
  border-radius: 20px;
  overflow: hidden;
  background: white;
  box-shadow:
    0 10px 30px rgba(0, 0, 0, 0.1),
    0 0 0 1px rgba(0, 0, 0, 0.05);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;

  &:hover {
    box-shadow:
      0 20px 40px rgba(0, 0, 0, 0.15),
      0 0 0 1px rgba(102, 126, 234, 0.2);
  }

  // 暗色模式适配
  &.dark\:bg-gray-800 {
    background: #1f2937;
    border-color: #374151;
  }
}

// 图片容器
.image-container {
  position: relative;
  height: 220px;
  overflow: hidden;
  cursor: pointer;

  .article-image {
    width: 100%;
    height: 100%;
    transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);

    :deep(.el-image__inner) {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .image-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  }

  // 悬停遮罩
  .image-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--gradient-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: all 0.3s ease;

    .read-more-btn {
      background: rgba(255, 255, 255, 0.2);
      border: 2px solid rgba(255, 255, 255, 0.3);
      border-radius: 50px;
      padding: 15px 25px;
      color: white;
      font-weight: 600;
      display: flex;
      align-items: center;
      gap: 10px;
      backdrop-filter: blur(10px);
      transform: translateY(20px);
      transition: all 0.3s ease;

      i {
        font-size: 16px;
      }
    }
  }

  &:hover {
    .article-image {
      transform: scale(1.1);
    }

    .image-overlay {
      opacity: 1;

      .read-more-btn {
        transform: translateY(0);
      }
    }
  }
}

// 卡片内容
.card-content {
  padding: 25px;
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(10px);
  position: relative;

  .dark & {
    background: rgba(31, 41, 55, 0.98);
  }
}

// 标签容器
.tags-container {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 15px;
  flex-wrap: wrap;

  .tag-item {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--gradient-primary);
    color: white;
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 500;
    box-shadow: 0 2px 10px rgba(102, 126, 234, 0.3);

    .tag-icon {
      width: 16px;
      height: 16px;
      border-radius: 50%;
    }

    .tag-name {
      white-space: nowrap;
    }
  }

  .more-tags {
    background: rgba(102, 126, 234, 0.1);
    color: #667eea;
    padding: 6px 10px;
    border-radius: 15px;
    font-size: 11px;
    font-weight: 600;
  }
}

// 文章标题
.article-title {
  font-size: 18px;
  font-weight: 700;
  color: #2d3748;
  line-height: 1.4;
  margin-bottom: 15px;
  cursor: pointer;
  transition: all 0.3s ease;

  // 多行文本截断
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-word;

  // 确保两行文本有足够空间
  min-height: 2.8em; // 1.4 line-height * 2 lines
  max-height: 2.8em;

  &:hover {
    color: #667eea;
    transform: translateY(-1px);
  }

  .dark & {
    color: #f7fafc;

    &:hover {
      color: #90cdf4;
    }
  }
}

// 文章描述
.article-desc {
  color: #64748b;
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 20px;

  // 多行文本截断
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-word;

  // 确保三行文本有足够空间
  min-height: 4.5em; // 1.5 line-height * 3 lines
  max-height: 4.5em;

  .dark & {
    color: #9ca3af;
  }
}

// 分隔线
.divider {
  width: 100%;
  height: 1px;
  background: linear-gradient(90deg, transparent, #e2e8f0, transparent);
  margin-bottom: 15px;

  .dark & {
    background: linear-gradient(90deg, transparent, #374151, transparent);
  }
}

// 元信息
.article-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: #64748b;

  .meta-item {
    display: flex;
    align-items: center;
    gap: 6px;

    i {
      color: #667eea;
      font-size: 12px;
    }

    .category-name {
      color: #667eea;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.3s ease;

      &:hover {
        color: #764ba2;
        text-decoration: underline;
      }
    }
  }

  .dark & {
    color: #9ca3af;

    .category-name {
      color: #667eea;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .image-container {
    height: 180px;
  }

  .card-content {
    padding: 20px;
  }

  .article-title {
    font-size: 16px;
    min-height: 2.4em; // 调整移动端高度
    max-height: 2.4em;
    line-height: 1.2;
  }

  .article-desc {
    font-size: 13px;
    min-height: 3.9em; // 调整移动端高度
    max-height: 3.9em;
    line-height: 1.3;
  }

  .article-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
}

// 光标样式
.image-container,
.article-title {
  cursor: var(--cursor-pointer, pointer);
}
</style>
